package com.onlineMIS.ORM.entity.headQ.inventoryFlow;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.onlineMIS.ORM.DAO.headQ.barCodeGentor.ProductBarcodeDaoImpl;
import com.onlineMIS.ORM.DAO.headQ.inventoryFlow.HeadQInventoryStoreDAOImpl;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.Year;
import com.onlineMIS.ORM.entity.chainS.user.ChainStore;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.Brand;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.Color;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.Product;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.Quarter;
import com.onlineMIS.common.Common_util;
import com.onlineMIS.common.ExcelTemplate;
import com.onlineMIS.sorter.ChainInventoryOrderProductSorter;

public class HeadqInventoryFlowOrderTemplate  extends ExcelTemplate{

	
	private HeadqInventoryFlowOrder order = new HeadqInventoryFlowOrder();
	private HeadQInventoryStore store ; 
	private boolean showCost;
	private int data_row = 3;


//    public ChainInventoryFlowOrderTemplate(File file) throws IOException{
//    	super(file);
//    }
	
	public HeadqInventoryFlowOrderTemplate(HeadqInventoryFlowOrder order, HeadQInventoryStore store, String templateWorkbookPath, boolean showCost) throws IOException{
		super(templateWorkbookPath + "\\HeadqInventoryFlowOrderTemplate.xls");	
		this.order = order;
		this.store = store;
		this.showCost = showCost;
	}
	
	/**
	 *  this is the function to inject the inventory order to Jinsuan order template
	 * @return
	 */
	public HSSFWorkbook process(){
		HSSFSheet sheet = templateWorkbook.getSheetAt(0);
	 	DetachedCriteria criteria = DetachedCriteria.forClass(HeadQInventoryStore.class);
	 	criteria.add(Restrictions.eq("id", this.order.getStoreId()));
			
		List<HeadqInventoryFlowOrderProduct> items = order.getProductList();
//		
		Row header2 = sheet.getRow(0);
		header2.createCell(1).setCellValue(order.getTypeHQS());
		header2.createCell(3).setCellValue(order.getId());
		header2.createCell(5).setCellValue(Common_util.dateFormat_f.format(new Date()));
		
		//write header
		Row header1 = sheet.getRow(1);
		header1.createCell(1).setCellValue(this.store.getName());
		header1.createCell(4).setCellValue(order.getComment());
		
		
		int i = 0;
		for (HeadqInventoryFlowOrderProduct levelFourItem : items){
			Row row = sheet.createRow(data_row + i);

			Product product = levelFourItem.getProductBarcode().getProduct();
			
			row.createCell(0).setCellValue("");
			
			row.createCell(1).setCellValue(levelFourItem.getProductBarcode().getBarcode());
			
			Color color = levelFourItem.getProductBarcode().getColor();
			String colorName = "";
			if (color == null) {
				row.createCell(2).setCellValue(product.getProductCode());
			}else {
				colorName = color.getName();
				row.createCell(2).setCellValue(product.getProductCode()+color.getName());	
			}
			
			row.createCell(3).setCellValue(product.getCategoryLevel1().getCategory_Name()+product.getCategory().getCategory_Name());

			row.createCell(4).setCellValue(product.getUnit());

			row.createCell(5).setCellValue(levelFourItem.getQuantity());
			
			row.createCell(6).setCellValue(product.getUnit());

			row.createCell(7).setCellValue(levelFourItem.getQuantity());
			

			row.createCell(8).setCellValue(product.getDiscount());
			

			row.createCell(9).setCellValue(product.getWholeSalePrice());
			
			row.createCell(10).setCellValue("");

			row.createCell(11).setCellValue("");

			row.createCell(12).setCellValue("");
			row.createCell(13).setCellValue("");
			
			String year = product.getYear().getYear();
			String quarter = product.getQuarter().getQuarter_Name();
			String brandName = product.getBrand().getBrand_Name();
			
			String productComment = year +"年" + brandName + quarter + "装";
			row.createCell(14).setCellValue(productComment);
			row.createCell(15).setCellValue(product.getSalesPrice());
			row.createCell(16).setCellValue(product.getBrand().getBrand_Name());
			row.createCell(17).setCellValue(product.getProductIdentity()+colorName);
			row.createCell(18).setCellValue(product.getNumPerHand());
			int shou = levelFourItem.getQuantity()/ product.getNumPerHand();
			if ((levelFourItem.getQuantity() % product.getNumPerHand()) > 0)
				shou += 1;
			row.createCell(19).setCellValue(shou);
			i++;
		}
		

		return templateWorkbook;
	}
	

}
